import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

public class BlockingQueueDemo {
    public static void main(String[] args) throws Exception{
//        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
        BlockingQueue<String> blockingQueue = new SynchronousQueue<>();
//        System.out.println(blockingQueue.add("a"));
//        System.out.println(blockingQueue.add("b"));
//        System.out.println(blockingQueue.add("c"));
//        System.out.println(blockingQueue.add("x"));//抛异常 java.lang.IllegalStateException: Queue full

//        System.out.println(blockingQueue.element());//队列首元素是谁
//
//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());//抛异常 java.util.NoSuchElementException

//                System.out.println(blockingQueue.offer("a"));
//                System.out.println(blockingQueue.offer("b"));
//                System.out.println(blockingQueue.offer("c"));
//                System.out.println(blockingQueue.offer("d"));
//                System.out.println(blockingQueue.peek());
//                System.out.println(blockingQueue.poll());
//                System.out.println(blockingQueue.poll());
//                System.out.println(blockingQueue.poll());
//                System.out.println(blockingQueue.poll());

//        blockingQueue.put("a");
//        blockingQueue.put("b");
//        blockingQueue.put("c");
//        System.out.println("-========");
//        blockingQueue.put("d");
//        blockingQueue.take();
//        blockingQueue.take();
//        blockingQueue.take();
//        System.out.println(blockingQueue.offer("q", 2L, TimeUnit.SECONDS));
//        System.out.println(blockingQueue.offer("q", 2L, TimeUnit.SECONDS));
//        System.out.println(blockingQueue.offer("q", 2L, TimeUnit.SECONDS));
//        System.out.println(blockingQueue.offer("q", 2L, TimeUnit.SECONDS));

        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+"\t put 1");
                blockingQueue.put("1");
                System.out.println(Thread.currentThread().getName()+"\t put 2");
                blockingQueue.put("2");
                System.out.println(Thread.currentThread().getName()+"\t put 3");
                blockingQueue.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"a").start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(5);//线程暂停五秒
                System.out.println(Thread.currentThread().getName()+"\t get"+blockingQueue.take());

                TimeUnit.SECONDS.sleep(5);//线程暂停五秒
                System.out.println(Thread.currentThread().getName()+"\t get"+blockingQueue.take());

                TimeUnit.SECONDS.sleep(5);//线程暂停五秒
                System.out.println(Thread.currentThread().getName()+"\t get"+blockingQueue.take());

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"b").start();

    }
}

